﻿namespace TestTool.Tests.Common.CommonUtils
{
    using System;
    using System.Collections.Generic;
    using System.Runtime.CompilerServices;
    using System.Text;
    using System.Xml;
    using TestTool.Tests.Definitions.Interfaces;

    public class TopicInfo
    {
        public static TopicInfo ConstructTopicInfo(XmlElement element)
        {
            List<XmlElement> list = new List<XmlElement> {
                element
            };
            for (System.Xml.XmlNode node = element.ParentNode; node != null; node = node.ParentNode)
            {
                list.Add(node as XmlElement);
            }
            TopicInfo info = null;
            Dictionary<string, string> dictionary = new Dictionary<string, string>();
            string str = string.Empty;
            for (int i = list.Count - 1; i >= 0; i--)
            {
                XmlElement element2 = list[i];
                string str2 = string.IsNullOrEmpty(element2.NamespaceURI) ? str : element2.NamespaceURI;
                string prefixOfNamespace = element2.GetPrefixOfNamespace(element2.NamespaceURI);
                if (!string.IsNullOrEmpty(prefixOfNamespace))
                {
                    if (dictionary.ContainsKey(prefixOfNamespace) && (str2 != dictionary[prefixOfNamespace]))
                    {
                        string format = "tns{0}";
                        int num2 = 1;
                        while (true)
                        {
                            string key = string.Format(format, num2);
                            if (!dictionary.ContainsKey(key))
                            {
                                prefixOfNamespace = key;
                                break;
                            }
                            num2++;
                        }
                    }
                    if (!dictionary.ContainsKey(prefixOfNamespace))
                    {
                        dictionary.Add(prefixOfNamespace, str2);
                    }
                }
                TopicInfo info2 = new TopicInfo {
                    Name = element2.LocalName,
                    Namespace = element2.NamespaceURI,
                    NamespacePrefix = prefixOfNamespace
                };
                info2.ParentTopic = info;
                str = str2;
                info = info2;
            }
            return info;
        }

        public static TopicInfo ConstructTopicInfo(EventsTopicInfo plainInfo)
        {
            Dictionary<string, string> dictionary = new Dictionary<string, string>();
            foreach (string str in plainInfo.NamespacesDefinition.Replace('"' + Environment.NewLine, "\" ").Split(new char[] { ' ' }))
            {
                if (!string.IsNullOrEmpty(str))
                {
                    string[] strArray2 = str.Split(new char[] { '=' });
                    dictionary.Add(strArray2[0], strArray2[1].Replace("\"", "").Replace(Environment.NewLine, "").Trim());
                }
            }
            TopicInfo info = null;
            string[] strArray3 = plainInfo.Topic.Split(new char[] { '/' });
            for (int i = 0; i < strArray3.Length; i++)
            {
                TopicInfo info2 = info;
                string[] strArray4 = strArray3[i].Split(new char[] { ':' });
                string str3 = string.Empty;
                string str4 = string.Empty;
                string str5 = string.Empty;
                if (strArray4.Length == 1)
                {
                    str3 = strArray4[0];
                    if (info2 != null)
                    {
                        str4 = info2.Namespace;
                    }
                }
                else
                {
                    str3 = strArray4[1];
                    if (!dictionary.ContainsKey(strArray4[0]))
                    {
                        throw new ApplicationException(string.Format("Prefix {0} not defined", strArray4[0]));
                    }
                    str4 = dictionary[strArray4[0]];
                    str5 = strArray4[0];
                }
                info = new TopicInfo {
                    Name = str3,
                    Namespace = str4,
                    ParentTopic = info2,
                    NamespacePrefix = str5
                };
            }
            return info;
        }

        public static string CreateTopicPath(XmlElement topicElement, TopicInfo topicInfo)
        {
            List<TopicInfo> list = new List<TopicInfo>();
            for (TopicInfo info = topicInfo; info != null; info = info.ParentTopic)
            {
                list.Add(info);
            }
            TopicInfo info2 = list[list.Count - 1];
            string str = string.Empty;
            if (!string.IsNullOrEmpty(info2.Namespace))
            {
                str = topicElement.GetNamespacePrefix(info2.Namespace, info2.NamespacePrefix);
            }
            string str2 = string.IsNullOrEmpty(str) ? info2.Name : string.Format("{0}:{1}", str, info2.Name);
            string str3 = info2.Namespace;
            for (int i = list.Count - 2; i >= 0; i--)
            {
                info2 = list[i];
                string ns = string.IsNullOrEmpty(info2.Namespace) ? str3 : info2.Namespace;
                str = topicElement.GetNamespacePrefix(ns, info2.NamespacePrefix);
                string str5 = string.IsNullOrEmpty(info2.NamespacePrefix) ? info2.Name : string.Format("{0}:{1}", str, info2.Name);
                str3 = ns;
                str2 = string.Format("{0}/{1}", str2, str5);
            }
            return str2;
        }

        public static TopicInfo ExtractTopicInfo(string topicString, System.Xml.XmlNode topicNode)
        {
            TopicInfo info = null;
            string[] strArray = topicString.Split(new char[] { '/' });
            for (int i = 0; i < strArray.Length; i++)
            {
                TopicInfo info2 = info;
                string[] strArray2 = strArray[i].Split(new char[] { ':' });
                string str2 = string.Empty;
                string namespaceOfPrefix = string.Empty;
                string str4 = string.Empty;
                if (strArray2.Length == 1)
                {
                    str2 = strArray2[0];
                    if (info2 != null)
                    {
                        namespaceOfPrefix = info2.Namespace;
                    }
                }
                else
                {
                    str2 = strArray2[1];
                    namespaceOfPrefix = topicNode.GetNamespaceOfPrefix(strArray2[0]);
                    str4 = strArray2[0];
                }
                info = new TopicInfo {
                    Name = str2,
                    Namespace = namespaceOfPrefix,
                    NamespacePrefix = str4,
                    ParentTopic = info2
                };
            }
            return info;
        }

        public string GetDescription()
        {
            List<TopicInfo> list = new List<TopicInfo>();
            for (TopicInfo info = this; info != null; info = info.ParentTopic)
            {
                list.Add(info);
            }
            Dictionary<string, string> dictionary = new Dictionary<string, string>();
            foreach (TopicInfo info2 in list)
            {
                if (!string.IsNullOrEmpty(info2.Namespace) && !dictionary.ContainsKey(info2.Namespace))
                {
                    dictionary.Add(info2.Namespace, info2.NamespacePrefix);
                }
            }
            string qName = null;
            qName = list[list.Count - 1].QName;
            for (int i = list.Count - 2; i >= 0; i--)
            {
                TopicInfo info3 = list[i];
                qName = string.Format("{0}/{1}", qName, info3.QName);
            }
            StringBuilder builder = new StringBuilder();
            bool flag = true;
            foreach (string str2 in dictionary.Keys)
            {
                string str3 = string.Format("  xmlns:{0}={1}", dictionary[str2], str2);
                if (flag)
                {
                    builder.Append(str3);
                    flag = false;
                }
                else
                {
                    builder.Append(Environment.NewLine);
                    builder.Append(str3);
                }
            }
            string str4 = builder.ToString();
            if (!string.IsNullOrEmpty(str4))
            {
                return string.Format("{0}, where {1}{2}", qName, Environment.NewLine, str4);
            }
            return qName;
        }

        private static List<TopicInfo> GetOrderedTopic(TopicInfo topicInfo)
        {
            List<TopicInfo> list = new List<TopicInfo>();
            for (TopicInfo info = topicInfo; info != null; info = info.ParentTopic)
            {
                list.Add(info);
            }
            return list;
        }

        public EventsTopicInfo GetPlainInfo()
        {
            List<TopicInfo> list = new List<TopicInfo>();
            for (TopicInfo info = this; info != null; info = info.ParentTopic)
            {
                list.Add(info);
            }
            Dictionary<string, string> dictionary = new Dictionary<string, string>();
            foreach (TopicInfo info2 in list)
            {
                if (!string.IsNullOrEmpty(info2.Namespace) && !dictionary.ContainsKey(info2.Namespace))
                {
                    string namespacePrefix = info2.NamespacePrefix;
                    dictionary.Add(info2.Namespace, namespacePrefix);
                }
            }
            string qName = list[list.Count - 1].QName;
            for (int i = list.Count - 2; i >= 0; i--)
            {
                TopicInfo info3 = list[i];
                qName = string.Format("{0}/{1}", qName, info3.QName);
            }
            StringBuilder builder = new StringBuilder();
            foreach (string str3 in dictionary.Keys)
            {
                builder.AppendLine(string.Format("{0}=\"{1}\"", dictionary[str3], str3));
            }
            string str4 = builder.ToString();
            return new EventsTopicInfo { Topic = qName, NamespacesDefinition = str4 };
        }

        public static bool TopicsMatch(TopicInfo actualTopic, TopicInfo expectedTopic)
        {
            bool flag = false;
            TopicInfo parentTopic = expectedTopic;
            TopicInfo info2 = actualTopic;
        Label_0006:
            if ((info2 == null) && (parentTopic == null))
            {
                return true;
            }
            if ((info2 != null) && (parentTopic != null))
            {
                flag = (info2.Namespace == parentTopic.Namespace) && (info2.Name == parentTopic.Name);
                if (flag)
                {
                    parentTopic = parentTopic.ParentTopic;
                    info2 = info2.ParentTopic;
                    goto Label_0006;
                }
            }
            return flag;
        }

        public string Name { get; set; }

        public string Namespace { get; set; }

        public string NamespacePrefix { get; set; }

        public TopicInfo ParentTopic { get; set; }

        private string QName
        {
            get
            {
                if (!string.IsNullOrEmpty(this.NamespacePrefix))
                {
                    return string.Format("{0}:{1}", this.NamespacePrefix, this.Name);
                }
                return this.Name;
            }
        }
    }
}

